package Epic;

/************************************************************
 * 
 * Print all valid phone numbers of length n subject to 
 * following constraints:
 * 
 *  1.If a number contains a 4, it should start with 4
 *  2.No two consecutive digits can be same
 *  3.Three digits (e.g. 7,2,9) will be entirely disallowed, 
 *  take as input
 *  
 * @author ZhongYin Zhang
 *
 */
public class ValidPhoneNumber {

	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		boolean isValid = validatePhone(443564828);
		//boolean isValid = validatePhone(443564888);
		//boolean isValid = validatePhone(943564808);
		//boolean isValid = validatePhone(443564808);
		//boolean isValid = validatePhone(123456789);
		if(isValid){
			System.out.println("Valid Phone");
		}else{
			System.out.println("Not a Valid Phone");
		}
	}
	
	/************************************************************
	 * 
	 * @param number
	 * @return
	 */
	static boolean validatePhone(int number){
		boolean isValid = false;
		String str = String.valueOf(number);
		// If not of length 9, invalid.
		if(str.length() !=9){
			
			System.out.println("InValid1");
			return isValid;
		}
		// If contains 2, 7 or 9, invalid.
		if(str.contains("2")||str.contains("7")||
				str.contains("9")){
			System.out.println("InValid2");
			return isValid;
		}
		// If contains 4 and do not start with 4, invalid.
		if(str.contains("4") && (str.indexOf('4') !=0)){
			System.out.println("InValid3");
			return isValid;
		}
		char previousChar = 'a';
		for(int i =0; i < str.length(); i++){
			if(str.charAt(i) == previousChar){
				System.out.println("InValid5");
				return isValid;
			}
			previousChar = str.charAt(i); 
		}
		isValid = true;
		return isValid;
	}
}